//
//  ShipuTableViewCell.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ShipuTableViewCell.h"
#import "PeopleCollectionViewCell.h"
#import "LRAFNetwork.h"
#import <AFNetworking.h>
#import "ShipuModel.h"
#import <UIImageView+WebCache.h>
@interface ShipuTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)UICollectionView *collectionView;
@end

@implementation ShipuTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [ super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       
        [self prepareData];
        [self CreatCollectionView];
        
    }
    return self;
}
-(void)prepareData
{
    NSString *str = @"http://api.izhangchu.com/?methodName=ShequRecommend&token=0&user_id=0&version=4.1";
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *arr  = responseObject[@"data"][@"shequ_talent"];
        self.dataArr = [ShipuModel modelHanderWithArray:arr];
        NSLog(@"~~~~~~~~~~~%@",_dataArr);
        [_collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
#pragma mark -在cell中写collectionView
-(void)CreatCollectionView
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    //cell大小
    layout.itemSize = CGSizeMake(( WIDTH*375 - WIDTH*40) /4 , HEIGHT*60);
    //行列间距
    //行
    layout.minimumLineSpacing = 10 * WIDTH;
    //列
    layout.minimumInteritemSpacing =10 * HEIGHT;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(10 * WIDTH,HEIGHT*20,WIDTH*30, HEIGHT*20);
    _collectionView =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH*375,  HEIGHT*120)  collectionViewLayout:layout];
   _collectionView .delegate = self;
   _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    [self.contentView addSubview:_collectionView];
   
    _collectionView.pagingEnabled = NO;
    [_collectionView registerClass:[PeopleCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  _dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PeopleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ShipuModel *m = _dataArr[ indexPath.row];
    cell.name.text = m. nick;
   [cell.img sd_setImageWithURL:[NSURL URLWithString:m.head_img]];
    cell.fans.text = [NSString stringWithFormat:@"粉丝:%@",m.tongji_be_follow .stringValue];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //self.darenBlock();
}
@end
